﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Monocle.DataProvider;
using Monocle.Models;

namespace Monocle.Controllers
{
    public class PageController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Details(Guid id)
        {
            return View();
        }

        public ViewResult Edit(Guid id, IDataProvider dataProvider)
        {
            var dto = dataProvider.GetPage(id);

            var tPageList = dataProvider.GetPageItemsByPage(dto.PK_tPage).OrderBy(x => x.PageOrder).ToList();
            var viewModel = new PageViewModel(dto);
            viewModel.PageItemList = new PageItemCollectionViewModel(tPageList);
            return View(viewModel);
        }

        [HttpPost]
        public ActionResult Edit(PageViewModel viewModel, IDataProvider dataProvider)
        {
            if (!ModelState.IsValid)
            {
                //The viewmodel is not valid. Return input and the view
                return View(viewModel);
            }
            else
            {
                //Get the old DTO from the database
                var dbDTO = dataProvider.GetPage(viewModel.ID);

                //Merge it with the new (update fields)
                UpdateModel(dbDTO);
                try
                {
                    //    Save the site to the database

                    viewModel.UpdateFromDTO(dataProvider.SavePage(dbDTO));
                }
                catch (Exception e)
                {
                    //        If an error happens add that to the modelstate and return view
                    ModelState.AddModelError("", "En fejl er opstået: " + e.Message);
                    return View(viewModel);
                }

                //Update PageItem order if changed
                if (viewModel.PageItemOrder != null)
                {
                    var pageItemGuids = viewModel.PageItemOrder.Split(',').ToList();
                    var pageItemList = pageItemGuids.Select(item => dataProvider.GetPageItem(new Guid(item))).ToList();

                    var order = 1;
                    foreach (var item in pageItemList)
                    {
                        item.PageOrder = order;
                        try
                        {
                            dataProvider.SavePageItem(item);
                        }
                        catch (Exception e)
                        {
                            //        If an error happens add that to the modelstate and return view
                            ModelState.AddModelError("", "En fejl er opstået: " + e.Message);
                            return View(viewModel);
                        }
                        order++;
                    }
                }

                //Redirect the user to somewhere else. Could be a success page
                ViewBag.Status = Monocle.Resources.Monocle.Generic_Saved;
                viewModel.PageItemList = new PageItemCollectionViewModel(dataProvider.GetPageItemsByPage(viewModel.ID).OrderBy(x => x.PageOrder).ToList());
                return View(viewModel);
            }

        }

        public ActionResult Delete(Guid id, IDataProvider dataProvider)
        {
            var dto = dataProvider.GetPage(id);

            dataProvider.DeletePage(dto);
            return RedirectToAction("Edit", "App", new { id = dto.FK_tApp });
        }

        public ViewResult Create(Guid id, IDataProvider dataProvider)
        {
            var viewModel = new PageViewModel();
            viewModel.FK_tApp = id;
            viewModel.ID = Guid.NewGuid();

            return View("Create", viewModel);
        }

        [HttpPost]
        public ActionResult Create(PageViewModel viewModel, IDataProvider dataProvider)
        {
            if (!ModelState.IsValid)
            {
                //The viewmodel is not valid. Return input and the view
                return View(viewModel);
            }
            else
            {
                try
                {
                    //    Save the site to the database
                    viewModel.ID = Guid.NewGuid();
                    dataProvider.SavePage(viewModel.ToDTO());
                }
                catch (Exception e)
                {
                    //        If an error happens add that to the modelstate and return view
                    ModelState.AddModelError("", "En fejl er opstået: " + e.Message);
                    return View(viewModel);
                }

                //Redirect the user to somewhere else. Could be a success page
                return RedirectToAction("Edit", "App", new { id = viewModel.FK_tApp });
            }

        }

        public JsonResult GetIconSetImageNames(String id)
        {
            var fileList = System.IO.Directory.GetFiles(Server.MapPath("/Content/images/category_images/" + id));
            var pathList = new List<String>();
            foreach (var file in fileList)
            {
                var fileName = System.IO.Path.GetFileName(file);
                var url = "/Content/images/category_images/" + id + "/" + fileName;
                pathList.Add(url);
            }
            return Json(pathList, JsonRequestBehavior.AllowGet);
        }
    }
}
